package com.jtlrm.ckd.mvp.view.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;

import com.base.sdk.base.net.CommonObserver;
import com.hyphenate.chat.EMClient;
import com.hyphenate.exceptions.HyphenateException;
import com.jtlrm.ckd.R;
import com.jtlrm.ckd.base.acitvity.TitleBarActivity;
import com.jtlrm.ckd.entity.event.UpdateGroupMessage;
import com.jtlrm.ckd.mvp.model.SuiFangModel;
import com.jtlrm.ckd.util.EventBusUtil;

import butterknife.BindView;

public class ChangeGroupNameActivity extends TitleBarActivity {

    @BindView(R.id.name)
    EditText groupNameE;
    String groupName;
    SuiFangModel suiFangModel;
    String groupId;
    String imGroupId;

    public static void goActivity(Context context, String groupName, String groupId, String imGroupId) {
        Intent intent = new Intent(context, ChangeGroupNameActivity.class);
        intent.putExtra("groupName", groupName);
        intent.putExtra("imGroupId", imGroupId);
        intent.putExtra("groupId", groupId);
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        groupName = getIntent().getStringExtra("groupName");
        groupId = getIntent().getStringExtra("groupId");
        imGroupId = getIntent().getStringExtra("imGroupId");
        super.onCreate(savedInstanceState);
    }

    @Override
    protected String getTitleText() {
        return "修改群名称";
    }

    @Override
    protected void setContentLayout() {
        setContentView(R.layout.activity_change_group_name);

    }

    @Override
    protected void initView() {
        suiFangModel = new SuiFangModel();
        titleBar.tvRight.setText("保存");
        titleBar.tvRight.setVisibility(View.VISIBLE);
        groupNameE.setText(groupName);
    }

    @Override
    protected void obtainData() {

    }

    @Override
    protected void initEvent() {
        titleBar.tvRight.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String content = getTextStr(groupNameE);
                if (notEmpty(content)) {
                    change(content);
                } else {
                    showToast("群名称不能为空");
                }
            }
        });
    }

    private void change(final String newName) {
        showLoadingDialog("修改中");
        suiFangModel.changeGroupName(groupId, newName, new CommonObserver<Object>() {
            @Override
            public void onError(int errType, String errMessage) {
                dismissLoadingDialog();
                showToast(errMessage);
            }

            @Override
            public void onResult(Object data) {
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            EMClient.getInstance().groupManager().changeGroupName(imGroupId, newName);
                            runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    dismissLoadingDialog();
                                    showToast("修改成功");
                                    UpdateGroupMessage message = new UpdateGroupMessage();
                                    message.setGroupName(newName);
                                    EventBusUtil.postEvent(message);
                                    finish();
                                }
                            });
                        } catch (HyphenateException e) {
                            runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    dismissLoadingDialog();
                                    showToast("修改失败");
                                }
                            });
                            e.printStackTrace();
                        }
                    }
                }).start();
            }
        }, lifecycleSubject);
    }
}
